package com.mumulx.check.web.config.exception;
import com.mumulx.check.service.util.RespBean;
import com.mumulx.check.service.util.exception.CustomException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

/*统一异常处理
 *  Created by IntelliJ IDEA.
 *  User: 木木
 *  Date: 2020/5/10
 *  Time: 17:56
 *
 * https://blog.csdn.net/yali_aini/article/details/84638747  -----thymeleaf/error文件夹
 */
@ControllerAdvice
public class ExceptionHandle {
    private static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);
    /*上传文件太大，报异常*/
    @ExceptionHandler(value = MaxUploadSizeExceededException.class)
    @ResponseBody
    public RespBean maxUploadSizeExceededException(Exception e) {
        logger.error("*********************************文件太大");
        return RespBean.error("文件长传太大！");
    }
    @ExceptionHandler(value = CannotGetJdbcConnectionException.class)
    @ResponseBody
    public RespBean cannotGetJdbcConnectionException(Exception e) {
        logger.error("*********************************数据库连接异常");
        return RespBean.error("数据库连接异常！");
    }
    @ExceptionHandler(value = CustomException.class)
    @ResponseBody
    public RespBean customExceptionHandle(CustomException e) {
        logger.error("*********************************"+e.getMessage());
        return RespBean.error(e.getMessage());
    }

    /*其他异常*/
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public RespBean otherException(Exception e) {
        logger.error("*********************************其他异常");
        logger.error(e.getMessage());
        e.printStackTrace();

        return RespBean.error("系统异常！");
    }
}
